//
// Created by hsj on 2022/3/8.
//

#include "t_stl.h"
namespace t_stl_test{
    void test(){
        //测试
        cout<<"STL test..."<<endl;
//        testVector();
//        tstCopy();
        testInsert();
    }
    //vector
    void testVector(){
        //template <class T,class Allocator = allocator<T>>
        //class vector{...
        //使用默认的：allocator<T>类，使用new delete
        const int NUM = 5;
        t_utils_test::T t_format;

        vector<int> ratings(NUM);
        vector<string> titles(NUM);
        for (int i = 0; i < NUM; ++i) {
           ratings[i] = i;
           titles[i] = string(t_format.int2str(i));
        }

        for (int i = 0; i < NUM; ++i) {
            cout<<ratings[i]<<" :"<<titles[i]<<endl;
        }

        //迭代器方式
        //实际是一个广义的指针
        //指向vector的第一个
        vector<int>::iterator  pit = ratings.begin();
        cout<<*pit<<endl;

        //函数地址：加不加取地址运算符‘&’都一样
        std::for_each(ratings.begin(),ratings.end(),testVectorShow);
//        std::for_each(ratings.begin(),ratings.end(),&testVectorShow);

        //C++11
        //引用方式,可以操作值
        for (int &t :ratings){
            t = t + 1;
        }
        for (int &t :ratings){
            cout<<"c++11 forEach(have  + 1):"<<t<<endl;
        }
    }
    void testVectorShow(int &s){
        cout<<"testVectorShow:"<<s<<endl;
    }

    //copy【正、反向迭代器】
    void tstCopy(){
       int casts[10] = {1,2,3,4,5,6,7,8,9,10};
       vector<int> va(10);
       std::copy(casts,casts + 10,va.begin());
       for (int &i: va){
         cout<<i<<endl;
       }
       cout<<"*********************************"<<endl;
       //反向输出
       vector<int> va002(10);
       std::copy(casts,casts + 10,va002.rbegin());
       for (int &i002: va002){
            cout<<i002<<endl;
       }
       //以上代码一次完成
       cout<<"*********************************"<<endl;
       vector<int> va003(10);
       std::copy(casts,casts + 10,va003.begin());
       vector<int>::reverse_iterator rit;
       for (rit = va003.rbegin();rit != va003.rend();++rit){
            cout<<*rit<<endl;
       }
    }

    //insert
    void testInsert(){
        string s1[4]{"fine","finish","fashion","fate"};
        string s2[2]{"busy","bats"};
        string s3[2]{"silly","singers"};
        vector<string> words(4);
        std::copy(s1,s1 + 4,words.begin());
        std::for_each(words.begin(),words.end(),testInsertOutPut);
        cout<<"*********************************"<<endl;

        std::copy(s2,s2 + 2,std::back_insert_iterator<vector<string>>(words));
        std::for_each(words.begin(),words.end(),testInsertOutPut);
        cout<<"*********************************"<<endl;


        //放到最前面
        std::copy(s3,s3 + 2,std::insert_iterator<vector<string>>(words,words.begin()));
        std::for_each(words.begin(),words.end(),testInsertOutPut);

    }
    void testInsertOutPut(const string &s){
        cout<<s<<endl;
    }
}